package net.zoneland.knowledge.utils;

import net.zoneland.knowledge.constant.Constants;
import net.zoneland.knowledge.model.LoginUserPojo;
import net.zoneland.knowledge.model.QaBankWithBLOBs;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;

/**
 * 题库问答方法工具
 */
public class QaBankUtils {

    /**
     * 文件制度（旧公文汇编）
     */
    private static final String GWHB = "GWHB";

    /**
     * 学习参考
     */
    private static final String XXCK = "XXCK";
    private static final String MBSC = "模板手册";

    /**
     * 校验问答题目是否需要被过滤
     * @param userIds
     * @param deptIds
     * @param category
     * @param title
     * @param loginUserPojo
     * @return
     */
    public static boolean checkQaBankToFilter(final String userIds, final String deptIds, final String category,
                                                final String title, final LoginUserPojo loginUserPojo) {
        return checkQaBankField(userIds, Arrays.asList(loginUserPojo.getUserID()))
                || checkQaBankField(deptIds, Arrays.asList(loginUserPojo.getDepartmentDN()))
                || checkPurviewEnum(category, title, loginUserPojo);
    }

    /**
     * 校验问答角色权限
     * @param category
     * @param title
     * @param loginUserPojo
     * @return
     */
    private static boolean checkPurviewEnum(final String category, final String title,
                                              final LoginUserPojo loginUserPojo){
        if (StringUtils.equals(category, GWHB)
                || (StringUtils.equals(category, XXCK) && StringUtils.equals(title, MBSC))) {
            return loginUserPojo.getPurviewEnums().size() > 1;
        }
        return true;
    }



    /**
     * 校验问答的其中一个权限字段
     * @param filed
     * @param userData
     * @return
     */
    private static boolean checkQaBankField(final String filed, final List<String> userData){
        return StringUtils.isNotBlank(filed)
                && CollectionUtils.isNotEmpty(userData)
                && CollectionUtils.containsAny(Arrays.asList(StringUtils.split(filed, Constants.STRING_SEMICOLON)), userData);
    }
}
